local fenji = fk.CreateSkill{
  name = "setam__za_fenji",
}

fenji:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(fenji.name) and player == target and not data.phase_end then
      return table.contains({Player.Draw, Player.Discard}, player.phase)
      and #player:getTableMark("@$setam__za_fenji") > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local prompt = Fk:translate("#setam__za_fenji-prompt")
    local tab = {
      [Player.Draw] = {"摸牌数", "造成伤害值"},
      [Player.Discard] = {"弃牌数", "目标数"},
    }
    prompt = string.format(prompt, tab[player.phase][1], tab[player.phase][2])

    local use = player.room:askToUseVirtualCard(player, {
      name = player:getViewAsCardNames("setam__za_fenji", player:getTableMark("@$setam__za_fenji")),
      skill_name = fenji.name,
      prompt = prompt,
      skip = true,
    })
    if use then
      event:setCostData(self, use)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local cost = event:getCostData(self)
    local card, targets = cost.card, cost.tos
    player.room:removeTableMark(player, "@$setam__za_fenji", card.name)
    player.room:useCard{
      from = player,
      tos = targets,
      card = card,
    }
  end,
})

fenji:addAcquireEffect(function(self, player, is_start)
  local mark = Fk:getAllCardNames("t")
  if not is_start then
    player.room.logic:getEventsOfScope(GameEvent.UseCard, 999, function (e)
      local use = e.data ---@type UseCardData
      if use.card.skillName == fenji.name then
        table.removeOne(mark, use.card.name)
      end
    end, Player.HistoryGame)
  end
  player.room:setPlayerMark(player, "@$setam__za_fenji", mark)
end)

fenji:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@$setam__za_fenji", 0)
end)

fenji:addEffect(fk.DrawNCards, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(fenji.name) and player == target then
      return player:usedSkillTimes(fenji.name, Player.HistoryPhase) ~= 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local n = #player.room.logic:getActualDamageEvents(999, function (e)
      local damage = e.data ---@type DamageData
      return damage.card and damage.card.skillName == fenji.name
    end)
    data.n = n
  end
})

fenji:addEffect(fk.EventPhaseProceeding, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(fenji.name) and player == target then
      return player:usedSkillTimes(fenji.name, Player.HistoryPhase) ~= 0
      and player.phase == Player.Discard
    end
  end,
  on_use = function (self, event, target, player, data)
    local n = 0
    local room = player.room
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
      local use = e.data ---@type UseCardData
      if use.card and use.card.skillName == fenji.name then
        n = #e.data:getAllTargets()
        return true
      end
    end, Player.HistoryPhase)

    local canDiscards = table.filter(
      player:getCardIds{ Player.Hand }, function(id)
        local card = Fk:getCardById(id)

        local status_skills = Fk:currentRoom().status_skills[MaxCardsSkill] or Util.DummyTable
        for _, skill in ipairs(status_skills) do
          if skill:excludeFrom(player, card) then
            return false
          end
        end
        return true
      end
    )
    if #canDiscards ~= 0 then
      n = math.min(n, #canDiscards)

      local toDiscard

      local dis_data = {
        num = n,
        min_num = n,
        include_equip = false,
        skillName = "phase_discard",
        pattern = ".",
      }
      local _, ret = room:askToUseActiveSkill(player, {
        skill_name = "discard_skill",
        prompt = "#setam__za_fenji-discard:::"..n,
        cancelable = false,
        extra_data = dis_data
      })

      if ret then
        toDiscard = ret.cards
      else
        toDiscard = table.random(canDiscards, n) ---@type integer[]
      end

      room:throwCard(toDiscard, "phase_discard", player, player)
    end
    data.phase_end = true
  end
})

Fk:loadTranslationTable{ ["#setam__za_fenji_discard"] = "奋计" }

Fk:loadTranslationTable{
  ["setam__za_fenji"] = "奋计",
  [":setam__za_fenji"] = "摸牌/弃牌阶段开始时，你可以视为使用一张未以此法使用过的普通锦囊牌，然后将此阶段摸牌/弃牌数改为该牌造成伤害值/目标数。",

  ["#setam__za_fenji-prompt"] = "奋计：可以视为使用一张普通锦囊牌，将此阶段%s改为该牌%s",
  ["@$setam__za_fenji"] = "奋计",
  ["#setam__za_fenji-discard"] = "奋计：请弃置 %arg 张牌",

  ["$setam__za_fenji1"] = "敌众我寡，怎可少谋？",
  ["$setam__za_fenji2"] = "临城据守，当出奇计。",
}

return fenji
